package test.applet;

import java.util.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.io.*;
import testpark.*;

public class TestAccesApplet extends JApplet
{
    private boolean isStandalone = false;
    private TestAccesParking testAccesParking;
    private Properties props;

    //Obtenir une valeur de paramètre
    public String getParameter(String key, String def)
    {
	return isStandalone ? props.getProperty(key, def) :(getParameter(key) != null ? getParameter(key) : def);
    }

    public void setStandalone(Properties props)
    {
	this.props = props;
	isStandalone = true;
    }

    //Construire l'applet
    public TestAccesApplet()
    {
	try
	{
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
	}
	catch(Exception e) {}
	testAccesParking = new TestAccesParking();
    }

    //Initialiser l'applet
    public void init()
    {
	try {
	    jbInit();
	}
	catch(Exception e) {
	    e.printStackTrace();
	}
    }

    //Initialiser le composant
    private void jbInit() throws Exception {
	this.getContentPane().setBackground(Color.gray);

	this.setSize(new Dimension(352, 282));
	testAccesParking.setBackground(Color.gray);
	testAccesParking.setLayout(null);
	this.getContentPane().add(testAccesParking, BorderLayout.CENTER);
    }

    //Démarrer l'applet
    public void start()
    {
	setUrlSuperviseur();
    }

    //Arrêter l'applet
    public void stop()
    {
       testAccesParking.setTest(false);
    }

    //Détruire l'applet
    public void destroy() {
    }

    //Obtenir les informations d'applet
    public String getAppletInfo() {
	return "Information applet";
    }

    //Obtenir les informations de paramètre
    public String[][] getParameterInfo() {
	return null;
    }

    //**************************************************************************

    //Méthode main
    public static void main(String[] args)
    {
	Properties props = getProperties(args);
	JFrame frame = new JFrame();
	TestAccesApplet applet = new TestAccesApplet();
	applet.setStandalone(props);
	//applet.isStandalone = true;
	applet.init();

	frame.setDefaultCloseOperation(3);//EXIT_ON_CLOSE == 3
	frame.setTitle("Test Acces Parking");
	frame.getContentPane().add(applet, BorderLayout.CENTER);
	frame.pack();
	frame.setResizable(false);
	Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
	frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
	frame.setVisible(true);

	applet.start();
    }

    //Initialiseur statique pour le paramètre LookAndFeel
    static
    {
	try
	{
	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
	}
	catch(Exception e) {}
    }

    private void setUrlSuperviseur()
    {
	String urlSuperviseur;
	String codeBase;
	String uriRpcTest;
	codeBase = getParameter("URL_BASE_SUPERVISEUR", "");
	//Le superviseur n'envoie pas ce parametre pour forcer l'applet a le trouver
	if (codeBase.equals("")) //la valeur n'a pas ete trouvee
	{
	    if (isStandalone)
		codeBase = getCodeBase().toString();
	    else codeBase = "http://localhost:5080/servlet/";// au cas ou ..
	}
	uriRpcTest = getParameter("URI_RPC_TEST", "appletrpc");
	urlSuperviseur = codeBase + uriRpcTest;
	testAccesParking.initUrls(isStandalone, urlSuperviseur);
    }

    public static Properties getProperties( String[] args)
    {
	boolean help = false;
	boolean verbose = false;
	String propertyFile = null;
	Properties props = new Properties();
	if( args.length > 2)
	{
	    help = true;
	}
	else if( args.length == 1)
	{
	    if( "-h".equals( args[0])) help = true;
	    else propertyFile = args[0];
	}
	else if( args.length == 2)
	{
	    propertyFile = args[1];
	    if( "-h".equals( args[0])) help = true;
	}
	if( help)
	{
	    System.out.println( "Borne Acces 0.2");
	    System.out.println( "Copyright (C) 2000,2001,2002  Gilles Petot Software.");
	    System.out.println();
	    System.out.println( "BorneAcces [-(h)] [properties_file]");
	    return null;
	}
	if( propertyFile != null)
	{
	    try
	    {
		File f = new File( propertyFile);
		if( !f.isAbsolute())
		{
		    f = new File( System.getProperty( "user.dir"), f.getPath());
		}
		props.load( new FileInputStream( f));
	    }
	    catch( IOException ioe)
	    {
		System.err.print( "Error reading property file '");
		System.err.print( propertyFile);
		System.err.println( "'.  Using defaults.");
	    }
	}
	return props;
    }

}